#include "stdafx.h"
#include "GameServer.h"

//===========================================================================
void PrintUsage( void ) {
    char application[ MAX_PATH ] = { 0, };
    ::GetModuleFileNameA(
        ::GetModuleHandle( NULL ),
        application,
        MAX_PATH
    );
    
    const char * usage = "Usage : %s port";
    printf( usage, application );
}

//===========================================================================
bool ParseCommand(
    _TCHAR *   commands[],
    unsigned   size,
    unsigned & port
) {
    if( 2 != size )
        return false;

    port = _wtoi( commands[ 1 ] );
    return true;
}

//===========================================================================
int _tmain(
    int      argc,
    _TCHAR * argv[]
) {
    unsigned int port = 0;
    if( !ParseCommand( argv, argc, port ) ) {
        PrintUsage();
        getchar();
        
        return -1;
    }

    GameServer server;
    server.Initialize( port );
    
    std::cout << "Are you ready to test my homework? If so, press <Enter>" << std::endl;
    std::cout << "And then, press <Enter> again when you want to stop this server." << std::endl;    
    getchar();

    server.Run();

    getchar();
    server.Stop();
    std::cout << "Server is stopped by you." << std::endl;
    ::Sleep( 1000 * 2 );
    
    return 0;
}